package com.fnzx.service.impl;

import com.fnzx.dao.DescribeInfoDao;
import com.fnzx.entity.DescribeInfoEntity;
import com.fnzx.entity.LinkOtherEntity;
import com.fnzx.service.DescribeInfoService;
import com.fnzx.service.LinkOtherService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service("describeInfoService")
public class DescribeInfoServiceImpl extends ServiceImpl<DescribeInfoDao, DescribeInfoEntity> implements DescribeInfoService {
    @Override
    public List<DescribeInfoEntity> getPageInfo(Integer fromPage) {
        if(fromPage==null){
            fromPage=1;
        }
        List<DescribeInfoEntity> infoEntities = this.list(new QueryWrapper<DescribeInfoEntity>().eq("from_page", fromPage));
        List<DescribeInfoEntity> collect = infoEntities.stream()
                .filter(info -> info.getParentId() == 0)
                .peek(info ->{
                        List<DescribeInfoEntity> children = getChildren(info.getId(), infoEntities);
                        if (children!=null && children.size()>0){
                            info.setChildrenEntities(children);
                        }
                    }
                ).collect(Collectors.toList());
        return collect;
    }

    private List<DescribeInfoEntity> getChildren(Long id, List<DescribeInfoEntity> infoEntities) {
        List<DescribeInfoEntity> collect = infoEntities.stream()
                .filter(describeInfoEntity -> describeInfoEntity.getParentId().equals(id))
                .peek(info -> {
                    List<DescribeInfoEntity> children = getChildren(info.getId(), infoEntities);
                    if (children!=null && children.size()>0){
                        info.setChildrenEntities(children);
                    }
                }).collect(Collectors.toList());
        return collect;
    }

}